//
//  HNTouchProcessor.m
//  HeroNuts
//
//  Created by Vicky on 5/15/11.
//  Copyright 2011 Tongji University. All rights reserved.
//

#import "HNTouchProcessor.h"
#import "HNGroundLogic.h"
#import "HNLogicPosition.h"
#import "GameScene.h"
#import "HNActorInfoCache.h"
#import "HNActionFlowCenter.h"

#define CURSOR 100

@interface HNTouchProcessor()

- (NSString *)cursorNameAtPosition:(CGPoint)position;
- (void)cleanCursorImage;
- (void)restore;
- (void)drawCursorWithName:(NSString *)cursorNname;
- (NSString *)cursorNameUnderSwordModeAtPosition:(CGPoint)position;
- (NSString *)cursorNameUnderNoneSwordModeAtPosition:(CGPoint)position;

@end

@implementation HNTouchProcessor

@synthesize reachablePositionsAroundTarget = reachablePositionsAroundTarget_;
@synthesize target = target_;
@synthesize targetLayer = targetLayer_;

- (id)initWithTargetLayer:(CCLayer *)layer
{
    if ((self = [super init])) {
        self.targetLayer = layer;
        cursorMode_ = kHNNoneCursor;
        directions_ = [[NSArray alloc] initWithObjects:@"topleft",@"topright",@"right",@"bottomright",
                       @"bottomleft",@"left",nil];
    }
    
    return self;
}

- (void)dealloc
{
    [reachablePositionsAroundTarget_ release];
    [directions_ release];
    [super dealloc];
}

- (void)hnTouchBeganAtPosition:(CGPoint)position
{
    previousPosition_ = position;
    NSString *cursorName = [self cursorNameAtPosition:position];
    [self drawCursorWithName:cursorName];
}

- (void)hnTouchMovedToPosition:(CGPoint)position
{
    //若逻辑点未变化，直接返回
    if (position.x==previousPosition_.x && position.y==previousPosition_.y) {
        return;
    }
    
    NSString *cursorName = [self cursorNameAtPosition:position];
    [self drawCursorWithName:cursorName];
       
    previousPosition_ = position;
}

- (void)hnTouchEndedAtPosition:(CGPoint)position
{
    [self cleanCursorImage];
        
    HNLogicPosition *destination = [[HNLogicPosition alloc] initWithPoint:previousPosition_];
    
    switch (cursorMode_) {
        case kHNArrowCursor:
            //[[HNActionFlowCenter sharedActionFlowCenter] currentActorShootAt:target_];
            break;
           
        case kHNFlyCursor:
            [[HNActionFlowCenter sharedActionFlowCenter] currentActorMoveTo:destination attack:nil];
            break;
            
        case kHNWalkCursor:
            [[HNActionFlowCenter sharedActionFlowCenter] currentActorMoveTo:destination attack:nil];
            break;
            
        case kHNSwordCursor:
            {
                destination.position = cursorPosition_;
                
                if ([[HNGroundLogic sharedGroundLogic] isPosition:destination.position 
                                                  occupiedByActor:[GameScene sharedGameScene].currentActor]) 
                {
                    [[HNActionFlowCenter sharedActionFlowCenter] currentActorMoveTo:nil attack:target_];
                }
                else
                {
                    [[HNActionFlowCenter sharedActionFlowCenter] currentActorMoveTo:destination attack:target_];
                }
            }
            break;
    }
    
    [destination release];
    
	//行动确定 消除阴影 使groundlayer不可触摸
	if (cursorMode_ != kHNNoneCursor) {
		[[GameScene sharedGameScene].groundLayer.mapGrid revertHighlight];
		[GameScene sharedGameScene].groundLayer.isTouchEnabled = NO;
	}	
    
    [self restore];
}

- (NSString *)cursorNameAtPosition:(CGPoint)position
{
    if (cursorMode_ == kHNSwordCursor) {
        return [self cursorNameUnderSwordModeAtPosition:position];
    }
    else {
        return [self cursorNameUnderNoneSwordModeAtPosition:position];
    }
}

- (NSString *)cursorNameUnderNoneSwordModeAtPosition:(CGPoint)position
{
    cursorPosition_ = position;
    self.target = [[HNGroundLogic sharedGroundLogic] actorAtPosition:position];
    HNActor *currentActor = [GameScene sharedGameScene].currentActor; 
    NSString *cursorName;
    
    //企图攻击
    if (target_) 
    {
        //点中队友及自己
        if (target_.leftSide == currentActor.leftSide) {
            cursorMode_ = kHNNoneCursor;
            cursorName = [NSString stringWithString:@"cur_none"];
        }
        else 
        {
            //远程兵
            if (currentActor.shots>0 && 1/*攻击方身边没有人*/) //远程兵暂时不考虑箭的方向
            {
                cursorMode_ = kHNArrowCursor;
                cursorName = [NSString stringWithString:@"cur_arrow"];
            }
            //非远程兵
            else 
            {
                self.reachablePositionsAroundTarget = 
                [[HNGroundLogic sharedGroundLogic] reachablePositionsAroundActor:target_];
                
                //可以进行近程攻击
                if (reachablePositionsAroundTarget_) 
                {
                    cursorMode_ = kHNSwordCursor;
                    
                    HNLogicPosition *logicCursorPosition = [self.reachablePositionsAroundTarget objectAtIndex:0];
                    cursorPosition_ = logicCursorPosition.position;
                    
                    int directionIndex = [[HNGroundLogic sharedGroundLogic] directionFromPosition:cursorPosition_ toActor:target_];
                    cursorName = [NSString stringWithFormat:@"cur_sword_%@",[directions_ objectAtIndex:directionIndex]];
                }
                //目标无法击中
                else {
                    cursorMode_ = kHNNoneCursor;
                    cursorName = [NSString stringWithString:@"cur_none"];
                }
            }
        }
    }
    //无攻击企图，只有移动企图
    else
    {
        //所点位置可以到达
        if ([[HNGroundLogic sharedGroundLogic] currentActorCanReachPosition:position]) 
        {
            if (currentActor.canFly) 
            {
                cursorMode_ = kHNFlyCursor;
                cursorName = [NSString stringWithString:@"cur_fly"];
            }
            else 
            {
                cursorMode_ = kHNWalkCursor;
                cursorName = [NSString stringWithString:@"cur_walk"];
            }
        }
        //所点位置无法到达
        else 
        {
            cursorMode_ = kHNNoneCursor;
            cursorName = [NSString stringWithString:@"cur_none"];
        }
    }
    
    return cursorName;
}

- (NSString *)cursorNameUnderSwordModeAtPosition:(CGPoint)position
{
    int count = [reachablePositionsAroundTarget_ count];
    
    for (int i=0; i<count; i++) {
        HNLogicPosition *currentReachablePosition = [reachablePositionsAroundTarget_ objectAtIndex:i];
        //所点位置在欲攻击对象周围一圈，且可以到达
        if (currentReachablePosition.position.x == position.x && currentReachablePosition.position.y == position.y) {
            cursorMode_ = kHNSwordCursor;
            cursorPosition_ = position;
            
            int directionIndex = [[HNGroundLogic sharedGroundLogic] directionFromPosition:position toActor:target_];
            NSString *cursorName = [NSString stringWithFormat:@"cur_sword_%@",[directions_ objectAtIndex:directionIndex]];
            
            return cursorName;
        }
    }
    
    return [self cursorNameUnderNoneSwordModeAtPosition:position];    
}

- (void)drawCursorWithName:(NSString *)cursorNname
{     
    [self cleanCursorImage];
    
    CGPoint originalPosition = [[HNActorInfoCache sharedActorInfoCache] originalPositionByActor:cursorNname];
    CGPoint offset = [HNLogicPosition distanceInPixelsFrom:ccp(0,0) to:cursorPosition_];
    CGPoint destination;
    destination.x = originalPosition.x + offset.x;
    destination.y = originalPosition.y + offset.y;
    
    NSString *fileName = [NSString stringWithFormat:@"%@.png",cursorNname];
    CCSprite *cursor = [CCSprite spriteWithSpriteFrameName:fileName];
    cursor.anchorPoint = ccp(0,1);
    cursor.position = destination;

    [targetLayer_ addChild:cursor z:1 tag:CURSOR];
}

- (void)cleanCursorImage
{
    if ([targetLayer_ getChildByTag:CURSOR]) {
        [targetLayer_ removeChildByTag:CURSOR cleanup:YES];
    }
}

- (void)restore
{
    cursorMode_ = kHNNoneCursor;
    target_ = nil;
    [reachablePositionsAroundTarget_ removeAllObjects];
}


@end
